home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / hash / must_align.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-25  |  1.6 KB  |  66 lines  |  [TEXT/R*ch]

  1. /* @(#)must_align.c    10.2 3/11/94 03:06:18 */
  2. /*
  3.  * must_align - determine if longs must be aligned
  4.  *
  5.  * This file was written by:
  6.  *
  7.  *     Landon Curt Noll  (chongo@toad.com)    chongo <was here> /\../\
  8.  *
  9.  * This code has been placed in the public domain.  Please do not 
  10.  * copyright this code.
  11.  *
  12.  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH  REGARD  TO
  13.  * THIS  SOFTWARE,  INCLUDING  ALL IMPLIED WARRANTIES OF MER-
  14.  * CHANTABILITY AND FITNESS.  IN NO EVENT SHALL  LANDON  CURT
  15.  * NOLL  BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  16.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM  LOSS  OF
  17.  * USE,  DATA  OR  PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  18.  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR  IN
  19.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <signal.h>
  24.  
  25. void buserr();        /* catch alignment errors */
  26.  
  27. main()
  28. {
  29.     char byte[2*sizeof(unsigned long)];    /* mis-alignment buffer */
  30.     unsigned long *p;    /* mis-alignment pointer */
  31.     int i;
  32.  
  33. #if !defined(MUST_ALIGN)
  34.     /* setup to catch alignment bus errors */
  35.     signal(SIGBUS, buserr);
  36.     signal(SIGSEGV, buserr);    /* some systems will generate SEGV instead! */
  37.  
  38.     /* mis-align our long fetches */
  39.     for (i=0; i < sizeof(long); ++i) {
  40.     p = (unsigned long *)(byte+i);
  41.     *p = i;
  42.     *p += 1;
  43.     }
  44.  
  45.     /* if we got here, then we can mis-align longs */
  46.     printf("#undef MUST_ALIGN\n");
  47.  
  48. #else
  49.     /* force alignment */
  50.     printf("#define MUST_ALIGN\n");
  51. #endif
  52.     exit(0);
  53. }
  54.  
  55.  
  56. /*
  57.  * buserr - catch an alignment error
  58.  */
  59. void
  60. buserr()
  61. {
  62.     /* alignment is required */
  63.     printf("#define MUST_ALIGN\n");
  64.     exit(0);
  65. }
  66.